package net.peelmeagrape.hibernate;

import junit.framework.TestCase;
import org.dom4j.DocumentException;
import org.dom4j.Document;
import org.custommonkey.xmlunit.XMLAssert;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.peelmeagrape.hibernate.xmlgen.XmlUtils;
import net.peelmeagrape.hibernate.config.HibernateAnnotationsProcessor;

public abstract class GenerateMappingsTestBase extends TestCase
{
    private HibernateAnnotationsProcessor annotationsProcessor;
    private static Log log = LogFactory.getLog(GenerateMappingsTestBase.class);

    protected void setUp() throws Exception
    {
        annotationsProcessor = new HibernateAnnotationsProcessor();
        annotationsProcessor.setDebugMode(false);
    }

    protected void assertAnnotationsGenerateXmlResource(String resourceName, Class aClass)
            throws DocumentException
    {
        Document mappingXml = annotationsProcessor.getMappingAsDom4j(aClass);
        mappingXml = XmlUtils.normaliseXml(mappingXml);
        Document expected = XmlUtils.loadResource(resourceName);

        XMLAssert.assertXMLEqual("Expected: \n"+XmlUtils.prettyPrint(expected)+
                "\n\nActual:\n"+XmlUtils.prettyPrint(mappingXml),
                XmlUtils.toDom(expected),
                XmlUtils.toDom(mappingXml));
    }

    protected void assertAnnotationsGenerateXml(String xml, Class aClass)
            throws DocumentException
    {
        Document mappingXml = annotationsProcessor.getMappingAsDom4j(aClass);
        log.info(XmlUtils.prettyPrint(mappingXml));
        mappingXml = XmlUtils.normaliseXml(mappingXml);

        Document expected = XmlUtils.parseXml(
                        "<!DOCTYPE hibernate-mapping PUBLIC\n" +
                "    \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n" +
                "    \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"><hibernate-mapping>"+
                xml+
                "</hibernate-mapping>");

        expected = XmlUtils.normaliseXml(expected);

        XMLAssert.assertXMLEqual("Expected: \n"+XmlUtils.prettyPrint(expected)+
                "\n\nActual:\n"+XmlUtils.prettyPrint(mappingXml),
                XmlUtils.toDom(expected),
                XmlUtils.toDom(mappingXml));
    }

}
